package www.ym.com.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import www.ym.com.common.R;
import www.ym.com.common.RL;
import www.ym.com.entity.Department;
import www.ym.com.entity.Role;
import www.ym.com.service.DepartmentService;
import www.ym.com.service.RoleService;
import www.ym.com.service.UserService;

import java.util.List;

@Slf4j
@RequestMapping("department")
@RestController
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private UserService userService;

    @Transactional
    @GetMapping("/list")
    public R<List<Department>> list(){
        LambdaQueryWrapper<Department> queryWrapper = new LambdaQueryWrapper<>();
        List<Department> list = departmentService.list(queryWrapper);
        return R.success(list,null);
    }

    //查询所有的部门
    @PostMapping("allDepartment")
    public R<List> allDepartment(){
        LambdaQueryWrapper<Department> queryWrapper = new LambdaQueryWrapper<>();
        List<Department> list = departmentService.list(queryWrapper);
        return  R.success(list,"");
    }

    /**
     * 分页查询
     *
     * @param jsonObject
     * @return
     */
    @PostMapping("/list")
    public RL<Department> page(@RequestBody JSONObject jsonObject) {
        Page<Department> userPage = departmentService.pageUser(jsonObject);
        List<Department> records = userPage.getRecords();
        long total = userPage.getTotal();
        long pageNum = userPage.getCurrent();
        long pageSize = userPage.getSize();
        return RL.listSuccess(records, null, total, pageNum, pageSize);}

    /**
     * 增加部门
     * @param department
     * @return
     */
    @PostMapping("/add")
    public R<String> addRole(@RequestBody Department department){
        departmentService.addDepartment(department);
        return R.success(null,"添加成功");
    }

    /**
     * 修改部门信息
     * @param department
     * @return
     */
    @PostMapping("/update")
    public R<String> updateRole(@RequestBody Department department){
        departmentService.updateDepartment(department);
        return R.success(null,"修改成功");
    }

    /**
     * 删除部门
     * @param department
     * @return
     */
    @PostMapping("/del")
    public R<String> del(@RequestBody Department department){
        LambdaQueryWrapper<Department> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(Role::getId,role.getId());
//        Role role1 = roleService.getOne(queryWrapper);

//        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(User::getRoleName,role1.getRoleName());
//        int count = userService.count(wrapper);
//        if (count>0){
//            throw new CustomException("角色已绑定人员，无法删除");
//        }
//
//        if ("超级管理员".equals(role1.getRoleName())) {
//            throw new CustomException("超级管理员无法删除");
//        }
        departmentService.removeById(department.getId());
        return R.success(null,"删除成功");

    }

}
